package com.forhappy.light3ds8.server;


import java.util.HashMap;
import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.forhappy.light3ds8.datatype.DataReal;
import com.forhappy.light3ds8.db.LedDBMgr;
import com.forhappy.light3ds8.server.HttpUtils.Result;
import com.forhappy.light3ds8.tools.Constant;

public class Job_SyncDataReal implements Runnable{
	private static final String TAG = "Job_SyncDataReal";
	Context mContext;
	int mId;
	public Job_SyncDataReal(Context context, int id){
		mContext = context;
		mId = id;
	}
	@Override
	public void run() {
		Log.d(TAG, "enter Job_SyncDataBasicInfo::run()");
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put("opType", "getReal");
		params.put("id", mId);
		Result res = HttpUtils.post(Constant.SERVER_DATA_URL, params);
		if(res.isSuccess()){
			LedDBMgr dbMgr = new LedDBMgr(mContext);
			try {
				JSONArray jsArray = new JSONArray(res.msg);
				int len= jsArray.length();
				if(null == jsArray || jsArray.length() == 0){
					Log.e(TAG, "get empty realData for idx:" + mId);
					return;
				}

				//一次性只能保存一个
				JSONObject jsObj = jsArray.getJSONObject(0);
				DataReal real = new DataReal(jsObj);
				real.id = mId;
				LinkedList<DataReal> dataRealList = new LinkedList<DataReal>();
				dataRealList.add(new DataReal(jsObj));

				dbMgr.saveToDBReal(dataRealList);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
	}

}
